//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include "credirection.h"
#include <buola/os/cprocess.h>
#include <buola/app/ccmdline.h>
#include <fcntl.h>

#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"

namespace buola { namespace bush {
    
CCmdLineFlag sDebugRedirections("debug-redirections",L"print debugging information for redirections");

void CRedirection::Apply(CProcess& pProcess)
{
    mParsedWord.ExpandSimple(mWord);
    std::string lExpanded;
    mWord.GetUnescaped(lExpanded);
    
    if(mOp==TOKEN_OP_LESS)
    {
        if(mDstFD==-1)
            mDstFD=0;
        
        mSrcFD=open(lExpanded.c_str(),O_RDONLY);
        pProcess.AddRedirection(mDstFD,mSrcFD);
        pProcess.AddParentClose(mSrcFD);
    }
    else if(mOp==TOKEN_OP_GREATER)
    {
        if(mDstFD==-1)
            mDstFD=1;
        
        mSrcFD=open(lExpanded.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666);
        pProcess.AddRedirection(mDstFD,mSrcFD);
        pProcess.AddParentClose(mSrcFD);
    }
    else if(mOp==TOKEN_OP_RIGHTSHIFT)
    {
        if(mDstFD==-1)
            mDstFD=1;
        
        mSrcFD=open(lExpanded.c_str(),O_CREAT|O_WRONLY|O_APPEND,0666);
        pProcess.AddRedirection(mDstFD,mSrcFD);
        pProcess.AddParentClose(mSrcFD);
    }
    else if(mOp==TOKEN_OP_GTAND)
    {
        if(mDstFD==-1)
            mDstFD=2;
        
        if(!mSrcFD)
            mSrcFD=to_<int>(lExpanded);

        if(mSrcFD==mDstFD)
            return;
        pProcess.AddRedirection(mDstFD,mSrcFD);
    }
    else if(mOp==TOKEN_OP_LTAND)
    {
        if(mDstFD==-1)
            mDstFD=0;
        
        if(!mSrcFD)
            mSrcFD=to_<int>(lExpanded);

        if(mSrcFD==mDstFD)
            return;
        pProcess.AddRedirection(mDstFD,mSrcFD);
    }
    else if(mOp==TOKEN_OP_OR) //generated by a pipeline
    {
        pProcess.AddRedirection(mDstFD,mSrcFD);
        pProcess.AddParentClose(mSrcFD);
    }
    else
    {
        msg_warn() << "unimplemented redirect op\n";
        return;
    }
}

io::PFDStream CRedirection::GetOutputStream()
{
    mParsedWord.ExpandSimple(mWord);
    std::string lExpanded;
    mWord.GetUnescaped(lExpanded);
    
    if(mOp==TOKEN_OP_GREATER)
    {
        if(mDstFD==-1||mDstFD==1)
        {
            mSrcFD=open(lExpanded.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666);
            return new io::CFDStream(mSrcFD);
        }
    }
    else if(mOp==TOKEN_OP_RIGHTSHIFT)
    {
        if(mDstFD==-1||mDstFD==1)
        {
            mSrcFD=open(lExpanded.c_str(),O_CREAT|O_WRONLY|O_APPEND,0666);
            return new io::CFDStream(mSrcFD);
        }
    }
    else if(mOp==TOKEN_OP_OR) //generated by a pipeline
    {
        return new io::CFDStream(mSrcFD);
    }
    
    return nullptr;
}

/*namespace bush*/ } /*namespace buola*/ }
